package com.hlk.emos;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlk.emos.wx.config.SystemConstants;
import com.hlk.emos.wx.db.dao.SysConfigDao;
import com.hlk.emos.wx.db.pojo.SysConfigEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.List;

@SpringBootApplication
public class EmosApplication {
    @Autowired
    private SysConfigDao sysConfigDao;

    @Autowired
    private SystemConstants constants;

    public static void main(String[] args) {
        SpringApplication.run(EmosApplication.class, args);
    }


    @PostConstruct
    public void init() {
        LambdaQueryWrapper<SysConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfigEntity::getStatus, 1);
        List<SysConfigEntity> list = sysConfigDao.selectList(wrapper);
        list.forEach(item -> {
            String key = StrUtil.toCamelCase(item.getParamKey());
            String value = item.getParamValue();
            try {
                Field field = constants.getClass().getDeclaredField(key);
                field.set(constants, value);
            } catch (Exception e) {
                e.printStackTrace();
            }

        });
    }

}
